package com.atguigu.yygh.hosp.service.impl;

import java.util.Map;
import java.util.Optional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.client.service.DictFeignClient;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;

/** 
* @author 作者 Your-Name: 
* @version 创建时间：2021年4月26日 下午3:47:40 
* 类说明 
*/
@Service
public class HospitalServiceImpl implements HospitalService{

	@Autowired
	private HospitalRepository hospitalRepository;
	
	@Autowired
	private DictFeignClient dictFeignClient;

	/**
	 * 上传医院接口
	 */
	@Override
	public void save(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		// map 集合转换成对象    先转换成字符串
		String json = JSONObject.toJSONString(paramMap);
		//转换成 Hospital 对象
		Hospital hosp = JSONObject.parseObject(json,Hospital.class);
		//1.判断是否存在数据
		String hostcode = hosp.getHostcode();
		Hospital hospitalExit = hospitalRepository.getHospitalByHostcode(hostcode);
		//2.如果存在  进行修改
		if(hospitalExit != null){
			hosp.setStatus(hospitalExit.getStatus());
			hosp.setCreateTime(hospitalExit.getCreateTime());
			hosp.setUpdateTime(new Date());
			hosp.setIsDeleted(0);
			//存储到数据库  修改操作
			hospitalRepository.save(hosp);
		}else{   //3.如果不存在  进行添加
			hosp.setStatus(0);
			hosp.setCreateTime(new Date());
			hosp.setUpdateTime(new Date());
			hosp.setIsDeleted(0);
			//存储到数据库  添加操作
			hospitalRepository.save(hosp);
		}	
	}

	/**
	 * 查询医院接口
	 */
	@Override
	public Hospital getByHostCode(String hostcode) {
		// TODO Auto-generated method stub
		Hospital hospital = hospitalRepository.getHospitalByHostcode(hostcode);
		return hospital;
	}

	/**
	 * 医院列表  条件查询 带分页
	 */
	@Override
	public Page<Hospital> selectHospitalPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
		// TODO Auto-generated method stub
		Pageable pageable = PageRequest.of(page - 1, limit);
		//创建 Example<T> 对象 条件匹配器
		ExampleMatcher marExampleMatcher = ExampleMatcher.matching()
			.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
		Hospital hospital = new Hospital();
		//hospitalQueryVo 转化称 hospital
		BeanUtils.copyProperties(hospitalQueryVo, hospital);
		//创建对象
		Example<Hospital> example = Example.of(hospital,marExampleMatcher);
		//分页
		Page<Hospital> hospList = hospitalRepository.findAll(example, pageable);
		//获取 全部数据 list
		List<Hospital> hosp =  hospList.getContent();
		//java8  流方式 遍历数据  进行医院等级的封装
		hosp.stream().forEach(item -> {
			this.setHospitalHosType(item);
		});
		
		return hospList;
	}
    
	/**
	 * 遍历  list 集合   对医院等级进行封装 
	 * @param hospital
	 * @return
	 */
	private Hospital setHospitalHosType(Hospital hospital) {
		//根据dict_code value 获取医院等级名称
		String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
		//查询  省 市 地区
		String province = dictFeignClient.getName(hospital.getProvinceCode());
		String city = dictFeignClient.getName(hospital.getCityCode());
		String district = dictFeignClient.getName(hospital.getDistrictCode());
		
		hospital.getParam().put("fullAddress", province+city+district);
		
		hospital.getParam().put("hostypeString", hostypeString);
		return hospital;
	}

	//更新医院上线状态
	@Override
	public void updateStatus(String id, Integer status) {
		// TODO Auto-generated method stub
		//根据 id 进行查询   
		Hospital hospital = hospitalRepository.findById(id).get();
		//将查询出的数据  status更新到数据库
		hospital.setStatus(status);
		hospital.setUpdateTime(new Date());
		hospitalRepository.save(hospital);
	}

	//医院详情信息
	@Override
	public Map<String, Object> getHospById(String id) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<>();
		Hospital hospital = hospitalRepository.findById(id).get();
		this.setHospitalHosType(hospital);
		map.put("hospital", hospital);
		//单独处理更直观
		map.put("bookingRule", hospital.getBookingRule());
		//不需要重复返回
		hospital.setBookingRule(null);
		return map;
		
	}

	/**
	 * 获取医院名称
	 */
	@Override
	public String getHostName(String hostcode) {
		// TODO Auto-generated method stub
		Hospital hospital = hospitalRepository.getHospitalByHostcode(hostcode);
		if(hospital.getHostname() != null){
			return hospital.getHostname();
		}
		return null;
	}
	

    /**
     * 根据医院名称进行查询
     */
	@Override
	public List<Hospital> getByHostname(String hostname) {
		// TODO Auto-generated method stub
		List<Hospital> hosp = hospitalRepository.getHospitalByHostnameLike(hostname);
		return hosp;
	}

	/**
	 * 根据医院编号  获取挂号详情信息
	 */
	@Override
	public Map<String, Object> getHospByHostcode(String hostcode) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<>();
		//医院详情
		Hospital hospital = this.setHospitalHosType(this.getByHostCode(hostcode));
		this.setHospitalHosType(hospital);
		map.put("hospital", hospital);
		//单独处理更直观
		map.put("bookingRule", hospital.getBookingRule());
		//不需要重复返回
		hospital.setBookingRule(null);
		return map;
	}
	
}
